﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using EzDesk.DesktopClient.ViewContract;
using EzDesk.DesktopClient.Event;


using EzDesk.Generic;
using EzDesk.Generic.WPF;
using EzDesk.DesktopClient.ViewModel;
using System.Windows.Threading;


namespace EzDesk.DesktopClient.View
{
    /// <summary>
    /// Interaction logic for FunctionPageView.xaml
    /// </summary>
    public partial class FunctionPageView : Window, IFunctionPageView
    {

        private DispatcherTimer _timerForAdjustSize;

        public object ViewModel
        {
            get
            {
                return this.DataContext;
            }
            set
            {
                this.DataContext = value;
            }
        }

        public dynamic Document
        {
            get
            {
                return WebContent.Document;
            }
        }

        public Double YOffsetWithMainWindow { get; set; }

        public FunctionPageView()
        {
            InitializeComponent();
            this.Dispatcher.BeginInvoke(new Action(this.Init));
        }

        private void Init()
        {
            InitProperty();
            InitWindowState();
            InitGlobalEvent();
            InitView();
            InitTimer();
        }

        private void InitTimer()
        {
            _timerForAdjustSize = new DispatcherTimer();
            _timerForAdjustSize.Interval = TimeSpan.FromMilliseconds(300);
            _timerForAdjustSize.Tick += new EventHandler(_timerForAdjustSize_Tick);
            _timerForAdjustSize.Start();
        }

        void _timerForAdjustSize_Tick(object sender, EventArgs e)
        {
            AdjustWindowSize();
        }

        private void InitProperty()
        {
            YOffsetWithMainWindow = 15;
        }

        private void InitWindowState()
        {
            this.ShowInTaskbar = true;
        }

        private void InitView()
        {
            //this.TopContent.Content = new TopPanelViewModel().View;
        }

        private void InitGlobalEvent()
        {
            MainWindowMoveEvent.Instance.Subscribe(this.OnMainWindowMove);
            MainWindowFocusEvent.Instance.Subscribe(this.OnMainWindowFocus);
        }

        private void OnMainWindowFocus(MainWindowFocusArgs args)
        {
            this.ToBeTop();
        }


        private void OnMainWindowMove(MainWindowMoveArgs args)
        {
            this.ToBeside(args.MainWindow as Window,yOffset:this.YOffsetWithMainWindow);
        }

        void IFunctionPageView.Navigate(string url)
        {
            WebContent.Navigate(url);
            this.ToBeTop();
            WebContent.LoadCompleted += WebContent_LoadCompleted;
        }

        void WebContent_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            WebContent.DisableDocumentScroll();
            AdjustWindowSize();
            AdjustWindowPosition();
        }

        private void AdjustWindowSize()
        {
            if (Document == null) return;
            this.Width = Math.Max(this.MinWidth, Convert.ToInt32(Document.Body.scrollWidth));
            LayoutRoot.Height = Document.Body.scrollHeight;
            this.Height = Math.Min(LayoutRoot.Height + 50, this.MaxHeight);
        }

        private void AdjustWindowPosition()
        {
            //if (this.IsVisible)
            //{
            //    this.ToBeTop();
            //    this.ToBesideAndAutoMove(IMClient.CurrentClient.MainView, yOffset: YOffsetWithMainWindow);
            //}
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void Top_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
